{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9aa9bbcf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "[1 2 3 4]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "list1 = [1,2,3,4]\n",
    "oneArray = np.array(list1)  #将python的内置数组转为ndarray数组\n",
    "print(type(oneArray))\n",
    "print(oneArray)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8e01f74e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "# 创建数组的多种形式\n",
    "# 1. 直接传入列表的方式\n",
    "t1 = np.array([1,2,3])\n",
    "print(t1)\n",
    "print(type(t1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d53ab5b3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "# 2. 传入range生成序列\n",
    "t2 = np.array(range(10))\n",
    "print(t2)\n",
    "print(type(t2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "224bde44",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 2 4 6 8]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "# 3. 使用numpy自带的np.arange()生成数组\n",
    "t3 = np.arange(0,10,2)\n",
    "print(t3)\n",
    "print(type(t3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5bc71883",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "#创建二维数组\n",
    "list2 = [[1,2],[3,4],[5,6]]\n",
    "twoArray = np.array(list2)\n",
    "print(twoArray)\n",
    "print(type(twoArray))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3d320477",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "(3, 2)\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "# 获取数组的维度( 注意： 与函数的参数很像)\n",
    "print(twoArray.ndim)\n",
    "# 形状（行，列）\n",
    "print(twoArray.shape)\n",
    "# 有多少个元素\n",
    "print(twoArray.size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b80fc319",
   "metadata": {},
   "outputs": [],
   "source": [
    "################################################调整数组的形状################################################"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b2a4f15a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "four = np.array([[1,2,3],[4,5,6]])\n",
    "four"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "06eb48ff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n"
     ]
    }
   ],
   "source": [
    "# 修改的是原有的\n",
    "four.shape = (3,2)  #注意不是转置！！！！\n",
    "print(four)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c1c07160",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n"
     ]
    }
   ],
   "source": [
    "# 返回一个新的数组\n",
    "fivr = four.reshape(3,2)\n",
    "print(fivr)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "45f0f5c4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 5],\n",
       "       [4, 3],\n",
       "       [2, 6]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将多维变成一维数组\n",
    "four = np.array([[1,2,3],[4,5,6]])\n",
    "five = four.reshape((3,2),order='F') #表示按列（Fortran风格）进行排列。Fortran风格的排列方式是按照列的顺序依次从原数组拿元素再按列填充新数组元素\n",
    "five"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "c631a3fb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 5]\n",
      " [4 3]\n",
      " [2 6]]\n",
      "[1 4 2 5 3 6]\n"
     ]
    }
   ],
   "source": [
    "six = four.flatten(order='F')\n",
    "print(five)\n",
    "print(six)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "5db47df1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]\n",
      "(24,)\n"
     ]
    }
   ],
   "source": [
    "# 拓展：数组的形状\n",
    "t = np.arange(24)\n",
    "print(t)\n",
    "print(t.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "02717b49",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "(4, 6)\n"
     ]
    }
   ],
   "source": [
    "# 转换成二维\n",
    "\n",
    "t1 = t.reshape((4,6))\n",
    "print(t1)\n",
    "print(t1.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "86d1c8f8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[9, 12, 88, 14, 25]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 将数组转成list\n",
    "a= np.array([9, 12, 88, 14, 25])\n",
    "list_a = a.tolist()\n",
    "print(list_a)\n",
    "print(type(list_a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "261cf936",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "int16\n"
     ]
    }
   ],
   "source": [
    "# 返回数组中每个元素的字节单位长度，dtype设置数据类型\n",
    "f = np.array([1,2,3,4,5], dtype = np.int16)\n",
    "print(f.itemsize)\n",
    "# 获取数据类型\n",
    "print(f.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "25e5dd79",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "int64\n"
     ]
    }
   ],
   "source": [
    "# 调整数据类型\n",
    "f1 = f.astype(np.int64)\n",
    "print(f1.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "82f830dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.82300977, 0.25067786, 0.12229359, 0.72303734, 0.13678652,\n",
       "       0.81300839, 0.40171213, 0.40465148, 0.52817295, 0.72241245])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import random\n",
    "arr = np.array([random.random() for i in range(10)])\n",
    "arr\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "bbcbe382",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.82 0.25 0.12 0.72 0.14 0.81 0.4  0.4  0.53 0.72]\n"
     ]
    }
   ],
   "source": [
    "# 取小数点后两位\n",
    "print(np.round(arr,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c3ac2f3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "#####################################################数组计算########################################################3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "f0b1778e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]\n",
      " [16 17 18 19]\n",
      " [20 21 22 23]]\n",
      "--------------------\n",
      "[[ 2  3  4  5]\n",
      " [ 6  7  8  9]\n",
      " [10 11 12 13]\n",
      " [14 15 16 17]\n",
      " [18 19 20 21]\n",
      " [22 23 24 25]]\n",
      "--------------------\n",
      "[[ 0  2  4  6]\n",
      " [ 8 10 12 14]\n",
      " [16 18 20 22]\n",
      " [24 26 28 30]\n",
      " [32 34 36 38]\n",
      " [40 42 44 46]]\n",
      "--------------------\n",
      "[[ 0.   0.5  1.   1.5]\n",
      " [ 2.   2.5  3.   3.5]\n",
      " [ 4.   4.5  5.   5.5]\n",
      " [ 6.   6.5  7.   7.5]\n",
      " [ 8.   8.5  9.   9.5]\n",
      " [10.  10.5 11.  11.5]]\n"
     ]
    }
   ],
   "source": [
    "t1 =np.arange(24).reshape((6,4)) #因numpy的广播机机制在运算过程中，加减乘除的值被广播到所有的元素上面\n",
    "print(t1)\n",
    "print(\"-\"*20)\n",
    "print(t1+2)\n",
    "print(\"-\"*20)\n",
    "print(t1*2)\n",
    "print(\"-\"*20)\n",
    "print(t1/2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "aad6c9b6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[100 102 104 106]\n",
      " [108 110 112 114]\n",
      " [116 118 120 122]\n",
      " [124 126 128 130]\n",
      " [132 134 136 138]\n",
      " [140 142 144 146]]\n",
      "[[   0  101  204  309]\n",
      " [ 416  525  636  749]\n",
      " [ 864  981 1100 1221]\n",
      " [1344 1469 1596 1725]\n",
      " [1856 1989 2124 2261]\n",
      " [2400 2541 2684 2829]]\n"
     ]
    }
   ],
   "source": [
    "# 同种形状的数组(对应位置进行计算操作)\n",
    "t1 = np.arange(24).reshape((6,4))\n",
    "t2 = np.arange(100,124).reshape((6,4))\n",
    "print(t1+t2)\n",
    "print(t1*t2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "a042e746",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[0 1 2 3 4 5]\n",
      "[[ 0  0  0  0  0  0]\n",
      " [ 6  6  6  6  6  6]\n",
      " [12 12 12 12 12 12]\n",
      " [18 18 18 18 18 18]]\n"
     ]
    }
   ],
   "source": [
    "# 行数或者列数相同的一维数组和多维数组可以进行计算：\n",
    "t1 = np.arange(24).reshape((4,6))\n",
    "t2 = np.arange(0,6)\n",
    "print(t1)\n",
    "print(\"-\"*20)\n",
    "print(t2)\n",
    "print(\"-\"*20)\n",
    "print(t1-t2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "f55643e6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "--------------------\n",
      "[[0]\n",
      " [1]\n",
      " [2]\n",
      " [3]]\n",
      "--------------------\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 5  6  7  8  9 10]\n",
      " [10 11 12 13 14 15]\n",
      " [15 16 17 18 19 20]]\n"
     ]
    }
   ],
   "source": [
    "t1 = np.arange(24).reshape((4,6))\n",
    "t2 = np.arange(4).reshape((4,1))\n",
    "print(t1)\n",
    "print(\"-\"*20)\n",
    "print(t2)\n",
    "print(\"-\"*20)\n",
    "print(t1-t2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "ad497485",
   "metadata": {},
   "outputs": [],
   "source": [
    "##################################################数组中的轴#######################################################3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "9134299d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 7 9]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.array([[1,2,3],[4,5,6]])\n",
    "print(np.sum(a,axis=0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "887500de",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 6 15]\n",
      "21\n"
     ]
    }
   ],
   "source": [
    "print(np.sum(a,axis = 1)) # [ 6 15]\n",
    "print(np.sum(a))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "06aa901e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2]\n",
      "  [ 3  4  5]\n",
      "  [ 6  7  8]]\n",
      "\n",
      " [[ 9 10 11]\n",
      "  [12 13 14]\n",
      "  [15 16 17]]\n",
      "\n",
      " [[18 19 20]\n",
      "  [21 22 23]\n",
      "  [24 25 26]]]\n",
      "--------------------\n",
      "[[27 30 33]\n",
      " [36 39 42]\n",
      " [45 48 51]]\n",
      "--------------------\n",
      "[[ 9 12 15]\n",
      " [36 39 42]\n",
      " [63 66 69]]\n",
      "--------------------\n",
      "[[ 3 12 21]\n",
      " [30 39 48]\n",
      " [57 66 75]]\n",
      "--------------------\n",
      "[[15 16 17]]\n"
     ]
    }
   ],
   "source": [
    "a=np.arange(27).reshape((3,3,3))\n",
    "print(a)\n",
    "b=np.sum(a, axis=0)\n",
    "print(\"-\"*20)\n",
    "print(b)\n",
    "c=np.sum(a, axis=1)\n",
    "print(\"-\"*20)\n",
    "print(c)\n",
    "d=np.sum(a, axis=2)\n",
    "print(\"-\"*20)\n",
    "print(d)\n",
    "print(\"-\"*20)\n",
    "print(a[[1],[2],:]) #三维索引取值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "6ccc742a",
   "metadata": {},
   "outputs": [],
   "source": [
    "##################################################数组的索引和切片####################################"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "12873204",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(10)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "0e90780b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 [0 1 2 3 4 5 6 7 8 9]\n"
     ]
    }
   ],
   "source": [
    "# 如果只放置一个参数，如 [2]，将返回与该索引相对应的单个元素\n",
    "print(a[2],a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "ec6b5bd6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 3 4 5 6 7 8 9]\n"
     ]
    }
   ],
   "source": [
    "# 如果为 [2:]，表示从该索引开始以后的所有项都将被提取\n",
    "print(a[2:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "baef3f37",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n",
      "[ 6  7  8  9 10 11]\n",
      "********************\n",
      "[ 6  7  8  9 10 11]\n"
     ]
    }
   ],
   "source": [
    "# 多维数组的操作方法\n",
    "import numpy as np\n",
    "t1 = np.arange(24).reshape(4,6)\n",
    "print(t1)\n",
    "print('*'*20)\n",
    "print(t1[1])\n",
    "print('*'*20)\n",
    "print(t1[1,:]) # 取一行\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "ade79120",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "********************\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "source": [
    "print('*'*20)\n",
    "print(t1[1:])# 取连续的多行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "7b4322ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "********************\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]]\n"
     ]
    }
   ],
   "source": [
    "print('*'*20)\n",
    "print(t1[1:3,:])# 取连续的多行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "a28a04c7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "********************\n",
      "[[ 0  1  2  3  4  5]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "source": [
    "print('*'*20)\n",
    "print(t1[[0,2,3]])# 取不连续的多行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "63450c69",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "********************\n",
      "[[ 0  1  2  3  4  5]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "source": [
    "print('*'*20)\n",
    "print(t1[[0,2,3],:])# 取不连续的多行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "f5cd4099",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "********************\n",
      "[ 1  7 13 19]\n"
     ]
    }
   ],
   "source": [
    "print('*'*20)\n",
    "print(t1[:,1])# 取一列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "0b2d8610",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "********************\n",
      "[[ 1  2  3  4  5]\n",
      " [ 7  8  9 10 11]\n",
      " [13 14 15 16 17]\n",
      " [19 20 21 22 23]]\n"
     ]
    }
   ],
   "source": [
    "print('*'*20)\n",
    "print(t1[:,1:])# 连续的多列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "10cf1f09",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "********************\n",
      "[[ 0  2  3]\n",
      " [ 6  8  9]\n",
      " [12 14 15]\n",
      " [18 20 21]]\n"
     ]
    }
   ],
   "source": [
    "print('*'*20)\n",
    "print(t1[:,[0,2,3]])# 取不连续的多列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "20240bcc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------\n",
      "15\n"
     ]
    }
   ],
   "source": [
    "print(\"-\"*20)\n",
    "print(t1[2,3])# # 取某一个值,三行四列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "92c38e30",
   "metadata": {},
   "outputs": [],
   "source": [
    "#####################################数组中的数值修改#######################################"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "4f51d270",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4,  5],\n",
       "       [ 6,  7,  8,  9, 10, 11],\n",
       "       [12, 13, 14, 15, 16, 17],\n",
       "       [18, 19, 20, 21, 22, 23]])"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = np.arange( 24 ).reshape( 4, 6 )\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "746dbad6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4,  5],\n",
       "       [ 0,  0,  0,  0,  0,  0],\n",
       "       [12, 13, 14, 15, 16, 17],\n",
       "       [18, 19, 20, 21, 22, 23]])"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# # 修改某一行的值\n",
    "t[1, :] = 0\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "5d78cf6d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  0,  2,  3,  4,  5],\n",
       "       [ 0,  0,  0,  0,  0,  0],\n",
       "       [12,  0, 14, 15, 16, 17],\n",
       "       [18,  0, 20, 21, 22, 23]])"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# # 修改某一列的值\n",
    "t[:, 1] = 0\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "94ce3ea9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4,  5],\n",
       "       [ 0,  0,  0,  0,  0,  0],\n",
       "       [ 0,  0,  0,  0,  0,  0],\n",
       "       [18, 19, 20, 21, 22, 23]])"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# # 修改连续多行\n",
    "t[1:3, :] = 0\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "bb89752e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  0,  0,  0,  4,  5],\n",
       "       [ 6,  0,  0,  0, 10, 11],\n",
       "       [12,  0,  0,  0, 16, 17],\n",
       "       [18,  0,  0,  0, 22, 23]])"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# # 修改连续多列\n",
    "t[:, 1:4] = 0  #修改一，二，三列\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "bddbf671",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4,  5],\n",
       "       [ 6,  7,  0,  0,  0, 11],\n",
       "       [12, 13,  0,  0,  0, 17],\n",
       "       [18, 19,  0,  0,  0, 23]])"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# # 修改多行多列，取第二行到第四行，第三列到第五列\n",
    "t[1:4,2:5]=0\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "65aac48d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 0  7  8  9 10 11]\n",
      " [12 13 14  0 16 17]\n",
      " [18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "source": [
    "# 修改多个不相邻的点\n",
    "t[[1,2],[0,3]]=0\n",
    "print(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "4d1ba5d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  0,  0,  0,  0,  0],\n",
       "       [ 0,  0,  0,  0, 10, 11],\n",
       "       [12, 13, 14,  0, 16, 17],\n",
       "       [18, 19, 20, 21, 22, 23]])"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 可以根据条件修改，比如讲小于 10 的值改掉\n",
    "t[t < 10] = 0\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "115da8a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  0,  0,  0],\n",
       "       [ 6,  7,  8,  9, 10, 11],\n",
       "       [12, 13, 14, 15, 16, 17],\n",
       "       [18, 19, 20, 21, 22, 23]])"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用逻辑判断\n",
    "t = np.arange( 24 ).reshape( 4, 6 )\n",
    "t[(t > 2) & (t < 6)] = 0 # 与\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "3103f925",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 2, 3, 4, 5],\n",
       "       [6, 0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0, 0]])"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = np.arange( 24 ).reshape( 4, 6 )\n",
    "t[(t < 2) | (t > 6)] = 0 # 或\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "bab49cb5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  0,  0,  0,  0,  0],\n",
       "       [ 0,  7,  8,  9, 10, 11],\n",
       "       [12, 13, 14, 15, 16, 17],\n",
       "       [18, 19, 20, 21, 22, 23]])"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = np.arange( 24 ).reshape( 4, 6 )\n",
    "t[~(t > 6)] = 0 # 非\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "ad0dff05",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[10, 10, 10, 10, 10, 10],\n",
       "       [10, 10, 10, 10, 10, 11],\n",
       "       [12, 13, 14, 15, 16, 17],\n",
       "       [18, 18, 18, 18, 18, 18]])"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = np.arange( 24 ).reshape( 4, 6 )\n",
    "t=t.clip(10,18) #小于10的数全部修改为10，大于18的全部修改为18\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "257d3f2f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 4, 4, 4, 4, 4, 5, 6, 7, 7, 7, 7, 7, 7])"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = np.array([1,2,3,4,4,4,5,6,7,7,7,8,9,9])\n",
    "t= t.clip(4,7)\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "65811506",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[False  True]\n",
      " [ True  True]\n",
      " [False  True]]\n"
     ]
    }
   ],
   "source": [
    "# # 拓 展\n",
    "# # 三目运算（ np.where(condition, x, y)满足条件(condition)，输出 x，不满足输出y\n",
    "score = np.array( [[80, 88], [82, 81], [75, 81]] )\n",
    "result = np.where( score > 80, True, False )\n",
    "print( result )\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2c4732e8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
